var ws, tid, doctorlist, studiolist;
document.addEventListener('plusready',function(){
	ws = plus.webview.currentWebview();
	tid = ws.tid;
	console.log(tid);
	var dtype = tid == 91 ? '0' : '1';
	showWating();
	//获取名医列表
	$.ajax({
		type :"get",
		url :apiRoot+"?m=home&c=studio&a=getDoctor",
		data :{dtype :dtype},
		success :function(data){
			plus.storage.setItem('dlist',data);
			doctorlist = $.parseJSON(data);
			searchDoctor(doctorlist);
		},
		error :function(){
			console.log(JSON.stringify(e));
			errortoast();
		}
	});
	console.log(apiRoot+"?m=home&c=studio&a=studioDoctor&tid="+tid);
	//获取工作室列表
	$.ajax({
		type:"get",
		url:apiRoot+"?m=home&c=studio&a=studioDoctor",
		data :{tid :tid},
		success : function(data){
			studiolist = $.parseJSON(data);
			plus.storage.setItem('melist',data);
			searchStudio(studiolist);
		},
		error : function(e){
			console.log(JSON.stringify(e));  
			errortoast();
		}
	});
	//搜索名医
//	$('.doctor').siblings('button').on('tap' , function(e){
	$('.doctor').on('input paste', function() {
		var search = $('.doctor').val();
		if(!search.trim()){
			$('.doctor').val('');
			searchDoctor(doctorlist);
			return;
		}
		$('.doctorList .famous-doctor-boss').each(function(k, v) {
			var _this = $(this);
			var name = _this.find('.name').text();
			if(name.indexOf(search) >= 0){
				_this.show();
			}else{
				_this.hide();
			}
		})
	})
	//搜索工作室
//	$('.studio').siblings('button').on('tap' , function(e){
	$('.studio').on('input paste', function() {
		var search = $('.studio').val();
		if(!search.trim()){
			$('.studio').val('');
			searchStudio(studiolist);
			return;
		}
		$('.studioList .famous-doctor-lists').each(function(k, v) {
			var _this = $(this);
			var name = _this.find('.font-15').text();
			if(name.indexOf(search) >= 0){
				_this.show();
			}else{
				_this.hide();
			}
		})
	})
},false)
function searchDoctor(arr){
	var str1 = '';
	$.each(arr, function(k, v) {
		plus.nativeUI.closeWaiting();
		console.log(JSON.stringify(v));
		str1 += '<div class="famous-doctor-boss mui-table-view-cell mui-table-view overflow-h margin-bottom-5" data-id="'+v.id+'">'+
					'<div><img src="'+getAvatar(v.avatar)+'" alt="" /></div>'+
					'<div>'+
						'<li class="mui-h4">'+
							'<span class="name">'+v.name+'</span>'+
							'<span class="font-15">　'+(v.resign?v.resign:'')+'</span>'+
						'</li>'+
						'<li class="mui-ellipsis-2">'+
							'<span>简介：</span>'+(v.experience?v.experience:'')+'</li>'+
					'</div>'+
				'</div>';
	});
	$('.doctorList').html(str1);
	$('.famous-doctor-boss').click(function(){
		goNewPage('find-doctor.html',{aid:$(this).attr('data-id')})
	})
}
function searchStudio(arr){
	var list = '';
	$.each(arr, function(k,v) {   
//				console.log(JSON.stringify(v.pic));
		list += '<div class="famous-doctor-lists" data-id="'+v.aid+'">'+
					'<div class="mui-table-view table-view-cell famous-doctor-icon">'+
						'<div><img src="'+getAvatar(v.pic)+'"/></div>'+
						'<div>'+
							'<li class="font-15">'+v.title+'</li>'+
						'</div>'+
					'</div>'+
					'<div class="table-view-cell background mui-h5 famous-doctor-list">'+
						'<h5 class="color-000">工作室介绍：</h5>'+
						'<span>'+(v.introduce).substring(0,500)+'</span>'+
					'</div>'+
				'</div>';
	});
	$('.studioList').html(list);
	plus.nativeUI.closeWaiting();
	$('.famous-doctor-lists').on('click',function(){
		goNewPage('home-famous-doctor-house.html',{aid:$(this).attr('data-id')});
	})
}
